package com.fortune.cat.resolver;

import com.fortune.cat.bean.RequestRateLimiterConfig;
import com.fortune.cat.enums.RateLimiterKeyResolverEnums;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

/**
 * @author zhaozeyang
 * @date 2019/4/26 11:30
 * @description
 */
@Component
public class HeaderKeyResolver extends AbstractKeyResolver {


    @Override
    public Integer getType() {
        return RateLimiterKeyResolverEnums.HEADER.getType();
    }

    @Override
    protected String doResolve(ServerWebExchange exchange) {
        RequestRateLimiterConfig.Header header = requestRateLimiterConfig.getHeader();
        if (header.getValue().equals(exchange.getRequest().getHeaders().getFirst(header.getHeader()))) {
            return header.getHeader().concat("-").concat(header.getValue());
        }
        return null;
    }
}
